package com.lbd99.data.lbd.controller;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lbd99.data.lbd.service.SysAccountService;
import com.lbd99.data.lbd.vo.SysAccount;
import com.lbd99.data.utils.StrUtil;

@Controller
@RequestMapping("account")
public class SysAccountController {

	@Autowired
	private SysAccountService sysAccountService;
	/**
	 * 登录
	 * @return
	 */
	@RequestMapping(value = "login")
	@ResponseBody	//userName为用户名或者手机号
	public Map<String, Object> login(String userName,String pwd,HttpSession httpSession) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("keyWord",userName);
		map.put("pwd", StrUtil.doEncrypt(pwd));
		resultMap=sysAccountService.login(map,httpSession);
		return resultMap;
	}

	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String gotoMainPage(HttpServletRequest request, HttpSession httpSession) {
		return "home/main";
	}


	@RequestMapping(value = "logout")
	public String logout(HttpSession httpSession) {
		httpSession.removeAttribute("SESSION_INFO_OBJECT");
		httpSession.removeAttribute("SESSION_INFO_OBJECT_ADMIN");
		return "redirect:/view/login.jsp";
	}


	/**
	 * 跳转到 基础面板 页面
	 */
	@RequestMapping(value = "/gotoBasePage", method = RequestMethod.GET)
	public String gotoBasePage(HttpServletRequest request, HttpSession httpSession) {

		return "home/basePanel";
	}

	/**
	 * 跳转到 凭证 页面
	 */
	@RequestMapping(value = "/gotoEvidencePage", method = RequestMethod.GET)
	public String gotoEvidencePage(Integer contentId,
			                       HttpServletRequest request,
								   HttpSession httpSession) {
		request.setAttribute("contentId",contentId);
		return "xd/evidence/evidence_list";
	}
	
	
	/**
	 * 修改密码
	 * @param oldPwd
	 * @param newPwd
	 * @param newPwdTrue
	 * @param id
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(value = "updatePwd")
	@ResponseBody
	public Map<String, Object> updatePwd(String oldPwd,String newPwd,String newPwdTrue,HttpSession httpSession) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String,Object> map=new HashMap<String,Object>();
		if(StringUtils.isEmpty(oldPwd) ||
				StringUtils.isEmpty(newPwd) ||
				StringUtils.isEmpty(newPwdTrue)){
			map.put("isSuc", false);
			map.put("msg", "参数不能为空!");
			return map;
		}
		if(!newPwd.equals(newPwdTrue)){
			map.put("isSuc", false);
			map.put("msg", "新输入的密码不一致!");
			return map;
		}
		SysAccount sysAccount = (SysAccount) httpSession.getAttribute("SESSION_INFO_OBJECT");
		if(sysAccount == null){
			map.put("isSuc", false);
			map.put("msg", "请先登录!");
			return map;
		}
		resultMap=sysAccountService.updatePwd(oldPwd,newPwd,sysAccount.getId());
		return resultMap;
	}
	
	 /**
     * 分页查询员工数据
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/getEmployee")
    @ResponseBody
    public Map<String, Object> getEmployee(int page,int rows,String phone) {
    	return sysAccountService.getEmployee(page,rows,phone);
    }
	
	 /**
     * 添加员工
     * @param sysAccount
     * @return
     */
    @RequestMapping(value = "/insertEmployee")
    @ResponseBody
    public Map<String,Object> insertEmployee(SysAccount sysAccount){
    	return sysAccountService.insertEmployee(sysAccount);
    }
    
    /**
     * 修改员工
     * @param sysAccount
     * @return
     */
    @RequestMapping(value = "/updateEmployee")
    @ResponseBody
    public Map<String,Object> updateEmployee(SysAccount sysAccount){
    	return sysAccountService.updateEmployee(sysAccount);
    }

}
